package com.ruoyi.security.convert;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.security.domain.SecurityGridTradingBacktestHistory;
import com.ruoyi.security.domain.SecurityGridTradingBacktestDayDetailsHistory;
import com.ruoyi.security.domain.SecurityGridTradingBacktestDetailsHistory;
import com.ruoyi.security.dto.backtest.SecurityGridTradingBacktestResultDto;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;


import static org.mapstruct.factory.Mappers.getMapper;

/**
 * @author yeoman
 * @date 2025/11/13 15:14
 * @description
 */
@Mapper(imports = {ShiroUtils.class, DateUtils.class})
public interface SecurityGridTradingBacktestHistoryConvert {

    SecurityGridTradingBacktestHistoryConvert INSTANCE = getMapper(SecurityGridTradingBacktestHistoryConvert.class);

    /**
     * 转换为日交易明细
     * @param resultDto 回测结果
     * @return 日交易明细实体
     */
    @Mappings({
            @Mapping(target = "userId", expression = "java(ShiroUtils.getUserId())"),
            @Mapping(target = "userName", expression = "java(ShiroUtils.getLoginName())"),
            @Mapping(target = "startDate", expression = "java(DateUtils.parseDate(resultDto.getStartDate()))"),
            @Mapping(target = "endDate", expression = "java(DateUtils.parseDate(resultDto.getEndDate()))"),
            @Mapping(target = "maxDrawdownStartDate", expression = "java(DateUtils.parseDate(resultDto.getMaxDrawdownStartDate()))"),
            @Mapping(target = "maxDrawdownEndDate", expression = "java(DateUtils.parseDate(resultDto.getMaxDrawdownEndDate()))"),
    })
    SecurityGridTradingBacktestHistory toSecurityGridTradingBacktestHistory(SecurityGridTradingBacktestResultDto resultDto);

    /**
     * 转换为日交易明细实体
     * @param dto  日交易DTO
     * @return 日交易明细实体
     */
    @Mappings({
            @Mapping(target = "date", expression = "java(DateUtils.parseDate(dto.getDate()))"),
    })
    SecurityGridTradingBacktestDayDetailsHistory toSecurityGridTradingBacktestHistoryDayDetails(SecurityGridTradingBacktestResultDto.DayDetail dto);

    /**
     * 转换为交易明细实体
     * @param dto  交易明细DTO
     * @return 交易明细实体
     */
    @Mappings({
            @Mapping(target = "time", source = "tradeTime"),
            @Mapping(target = "type", source = "tradeType"),
            @Mapping(target = "price", source = "tradePrice"),
            @Mapping(target = "quantity", source = "tradeQuantity"),
            @Mapping(target = "chargeAmount", source = "tradeChargeAmount"),
    })
    SecurityGridTradingBacktestDetailsHistory toSecurityGridTradingBacktestHistoryDetails(SecurityGridTradingBacktestResultDto.DayDetail.Detail dto);
}
